﻿using System;

namespace AopDemo
{

    #region 静态代理

    /// <summary>
    /// 装饰器类
    /// </summary>
    public class UserProcessorDecorator : IUserProcessorByStaticProxy
    {
        public IUserProcessorByStaticProxy UserProcessor { get; set; }

        public UserProcessorDecorator(IUserProcessorByStaticProxy userprocessor)
        {
            UserProcessor = userprocessor;
        }

        public void RegUser(User user)
        {
            PreProceed(user);
            UserProcessor.RegUser(user);
            PostProceed(user);

        }

        public void PreProceed(User user)
        {
            Console.WriteLine("方法执行前");
        }

        public void PostProceed(User user)
        {
            Console.WriteLine("方法执行后");
        }
    }
    /// <summary>
    /// 接口
    /// </summary>
    public interface IUserProcessorByStaticProxy
    {
        void RegUser(User user);
    }

    /// <summary>
    /// 实现类
    /// </summary>
    public class UserProcessorByStaticProxy : IUserProcessorByStaticProxy
    {
        public void RegUser(User user)
        {
            //todo 业务逻辑
            Console.WriteLine("StaticProxyClient 用户已注册。Name:{0},PassWord:{1}", user.Name, user.PassWord);
        }
    }

    public class StaticProxyClient
    {
        public static void Run()
        {
            try
            {
                User user = new User() {Name = "lee", PassWord = "123123123123"};
                IUserProcessorByStaticProxy userprocessor = new UserProcessorDecorator(new UserProcessorByStaticProxy());
                userprocessor.RegUser(user);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    

    #endregion
}
